package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuecheng.content.dto.BindTeachplanMediaDto;
import com.xuecheng.content.dto.SaveTeachplanDto;
import com.xuecheng.content.dto.TeacherPlanDto;
import com.xuecheng.content.entity.Teachplan;
import com.xuecheng.content.entity.TeachplanMedia;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.service.TeachPlanService;
import com.xuecheng.exception.XueChengPlusException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName TeachPlanServiceImpl
 * @Description 课程计划管理相关接口实现
 * @Author CG
 * @Date 2023/11/26 21:14
 **/
@Service("teachPlanService")
public class TeachPlanServiceImpl implements TeachPlanService {

    @Resource
    private TeachplanMapper teachplanMapper;


    @Resource
    private TeachplanMediaMapper teachplanMediaMapper;
    @Override
    public List<TeacherPlanDto> findTeacherPlanTree(Long courseId) {
        return teachplanMapper.selectTreeNodes(courseId);
    }



    private Integer getCount(Long parentid,Long courseId){
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        //拼接参数
        queryWrapper = queryWrapper.eq(Teachplan::getCourseId, courseId).eq(Teachplan::getParentid, parentid);
        Integer count = teachplanMapper.selectCount(queryWrapper);
        return  count;
    }
    @Override
    public void saveTeachplan(SaveTeachplanDto teachplanDto) {
        //通过课程计划id判断是新增还是修改
        Long teachplanId = teachplanDto.getId();
        if(teachplanId == null){
            //新增
            //封装数据
            Teachplan teachplan = new Teachplan();
            BeanUtils.copyProperties(teachplanDto,teachplan);
            //确定排序的字段,找到他的同级节点的个数,排序字段就是个数+1
            Long parentid = teachplanDto.getParentid();
            Long courseId = teachplanDto.getCourseId();
            /*LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
            //拼接参数
            queryWrapper = queryWrapper.eq(Teachplan::getCourseId, courseId).eq(Teachplan::getParentid, parentid);
            Integer count = teachplanMapper.selectCount(queryWrapper);*/

            teachplan.setOrderby(getCount(parentid,courseId)+1);

            teachplanMapper.insert(teachplan);

        }else{
            //修改
            Teachplan teachplan = teachplanMapper.selectById(teachplanId);
            BeanUtils.copyProperties(teachplanDto,teachplan);
            teachplanMapper.updateById(teachplan);


        }
    }

    @Override
    public void deleteTeachplan(Long id) {
        //通过课程计划id查询课程计划对象
        Teachplan teachplan = teachplanMapper.selectById(id);
        if(teachplan.getGrade() == 1){
            //章节
            List<Teachplan> childTeachplans = teachplanMapper.selectList(
                    new QueryWrapper<Teachplan>().eq("parentid", teachplan.getParentid()));
            if (childTeachplans.isEmpty()) {
                // 大章节下面没有了
                // 直接删除
                teachplanMapper.deleteById(teachplan);
            }else{
                //大章节下面还有小节没有删除完，异常
                XueChengPlusException.cast("课程计划信息还有子级信息，无法操作");
            }
        }else{
            //章节下面的小节
            //删除大章节下面的小节
            teachplanMapper.deleteById(teachplan);
        }
    }

    @Transactional
    @Override
    public TeachplanMedia associationMedia(BindTeachplanMediaDto bindTeachplanMediaDto) {
        //教学计划id
        Long teachplanId = bindTeachplanMediaDto.getTeachplanId();
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        if(teachplan == null){
            XueChengPlusException.cast("教学计划不存在");
        }
        Integer grade = teachplan.getGrade();
        if(grade!=2){
            XueChengPlusException.cast("只允许第二级教学资源绑定媒资文件");
        }
        //获取课程id
        Long courseId = teachplan.getCourseId();
        //先删除原来的教学计划
        LambdaQueryWrapper<TeachplanMedia> eq = new LambdaQueryWrapper<TeachplanMedia>().eq(TeachplanMedia::getTeachplanId, teachplanId);

        teachplanMediaMapper.delete(eq);
        //再添加教学计划与媒资的绑定关系
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        teachplanMedia.setCourseId(courseId);
        teachplanMedia.setTeachplanId(teachplanId);
        teachplanMedia.setMediaFilename(bindTeachplanMediaDto.getFileName());
        teachplanMedia.setMediaId(bindTeachplanMediaDto.getMediaId());
        teachplanMedia.setCreateDate(LocalDateTime.now());
        teachplanMediaMapper.insert(teachplanMedia);
        return teachplanMedia;
    }


}
